<?php

class TagController extends GxController {

  public $pageTitle = "Tags Editor";
  public $backurl;
  public $view_css = "/css/views/tag.css";



	public function actionCreate() {
		$this->allowUsers(Array('admin','superuser'),Array());
		$model = new Tag;


		if (isset($_POST['Tag'])) {
			$model->setAttributes($_POST['Tag']);
			$relatedData = array(
				'drills' => $_POST['Tag']['drills'] === '' ? null : $_POST['Tag']['drills'],
				);

			if ($model->saveWithRelated($relatedData)) {
				if (Yii::app()->getRequest()->getIsAjaxRequest())
					Yii::app()->end();
				else
					$this->redirect(array('admin'));
			}
		}

        $this->pageTitle .= " : New";
        $this->layout = '/layouts/main';
        $this->backurl = "admin";
		$this->render('create', array( 'model' => $model));
	}



	public function actionUpdate($id) {
        $this->allowUsers(Array('admin','superuser'),Array());
		$model = $this->loadModel($id, 'Tag');


		if (isset($_POST['Tag'])) {
			$model->setAttributes($_POST['Tag']);
			$relatedData = array(
				'drills' => $_POST['Tag']['drills'] === '' ? null : $_POST['Tag']['drills'],
				);

			if ($model->saveWithRelated($relatedData)) {
				$this->redirect(array('admin'));
			}
		}
        $this->pageTitle .= " : \"".$model->id."\"";
        $this->layout = '/layouts/main';
        $this->backurl = "admin";
		$this->render('update', array(
				'model' => $model,
				));
	}



	public function actionDelete($id) {
        $this->allowUsers(Array('admin','superuser'),Array());
		if (Yii::app()->getRequest()->getIsPostRequest()) {
			$this->loadModel($id, 'Tag')->delete();

			if (!Yii::app()->getRequest()->getIsAjaxRequest())
				$this->redirect(array('admin'));
		} else
			throw new CHttpException(400, Yii::t('app', 'Your request is invalid.'));
	}



	public function actionIndex() {
        $this->allowUsers(Array('admin','superuser'),Array());
		$this->redirect(array('admin'));
	}



	public function actionAdmin() {
        $this->allowUsers(Array('admin','superuser'),Array());
		$model = new Tag('search');
		$model->unsetAttributes();

		if (isset($_GET['Tag']))
			$model->setAttributes($_GET['Tag']);

        //$this->pageTitle = "Tags Management";
        $this->layout = '/layouts/main';
		$this->render('admin', array(
			'model' => $model,
		));
	}


    public function actionPdfExport() {

        $this->allowUsers(Array('admin','superuser'),Array());
        $model = new Tag('search');
        $this->layout = 'none';
        $this->render('pdfexport', array(
            'model' => $model,
        ));
    }

    
    public function actionExcelExport() {

        $this->allowUsers(Array('admin','superuser'),Array());
        $model = new Tag('search');
        $this->layout = 'none';
        $this->render('excelexport', array(
            'model' => $model,
        ));
    }
    


}